package me.jiatao.wan.paging3.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.RecyclerView
import com.hi.dhl.binding.databind
import me.jiatao.wan.R
import me.jiatao.wan.databinding.ItemArticleBinding
import me.jiatao.wan.model.ArticleModel

/**
 * @Author: taojia
 * @CreateDate: 2021/8/27 6:05 下午
 * @Version: 1.0
 * @Description:
 */

class HomeArticleAdapter : PagingDataAdapter<ArticleModel, HomeArticleAdapter.ViewHolder>(ArticleModel.diffCallback) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_article, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindTo(getItem(position), position)
    }

    class ViewHolder(parent: View) : RecyclerView.ViewHolder(parent) {

        private val mBinding: ItemArticleBinding by databind()

        fun bindTo(articleModel: ArticleModel?, position: Int) {
            articleModel?.let {
                mBinding.position = position
                mBinding.articleModel = it
                mBinding.executePendingBindings()
            }
        }
    }
}